package org.example.blog.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.blog.pojo.*;
import org.example.blog.service.impl.IndexServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.yaml.snakeyaml.events.Event;


import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private IndexServiceImpl indexService;
    @Autowired
    private UploadController uploadController;
    // 查询所有快速导航
    @GetMapping("/fastGuid")
    public Result list(){
        List<FastNavigation> fastList =  indexService.list();
        return Result.success(fastList);
    }

    //新增快速导航
    @PostMapping("/fastGuid")
    public Result add(@RequestBody FastNavigation fastNavigation){
        indexService.add(fastNavigation);
        return Result.success();
    }

    //删除导航栏
    @DeleteMapping("/fastGuid/{id}")
    public Result delete(@PathVariable Integer id){
        indexService.delete(id);
        return Result.success();
    }

    //更新数据
    @PutMapping("/fastGuid")
    public Result update(@RequestBody FastNavigation fastNavigation){
        indexService.update(fastNavigation);
        return Result.success();
    }

    //上传轮播图图片
    @PostMapping("/picture")
    public Result upload(MultipartFile file) throws IOException {
        String path =  uploadController.upload(file);
        if(path == "图片上传失败")
            return Result.error("200");
        indexService.upload(path);
        return Result.success();
    }
    //上传图片
    @PostMapping("/upload")
    public Result upload1(MultipartFile file) throws IOException {
        String path =  uploadController.upload(file);
        if(path == "图片上传失败")
            return Result.error("200");
        return Result.success(path);
    }
    // 查询照片
    @GetMapping("/picture")
    public Result list1(){
        List<Slideshow> slideshows =  indexService.list1();
        return Result.success(slideshows);
    }

    //删除图片
    @DeleteMapping("/picture/{id}")
    public Result delete1(@PathVariable Integer id){
        indexService.delete1(id);
        return Result.success();
    }

    //修改图片
    @PutMapping("/picture")
    public Result update1(@RequestBody Slideshow slideshow){
        indexService.update1(slideshow);
        return Result.success();
    }

    //首页文章的分页查询
    @GetMapping("/article")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize
                       ){
        log.info("分页查询：{}，{}",page,pageSize);
        PageBean bean = indexService.list2(page, pageSize);
        return Result.success(bean);
    }
    //首页文章的添加
    @PostMapping("/article")
    public Result add2(@RequestBody ArticleMs articleMs){
        indexService.add2(articleMs);
        return Result.success();
    }
    //首页文章的删除
    @DeleteMapping("/article/{id}")
    public Result deleteById(@PathVariable Integer id){
        indexService.deleteById(id);
        return Result.success();
    }
    //首页文章的修改
    @PutMapping("/article")
    public Result update2(@RequestBody ArticleMs articleMs){
        indexService.update2(articleMs);
        return Result.success();
    }
    //增加点击量
    @PutMapping("/hits")
    public Result update3(@RequestParam Integer id,
                          @RequestParam Integer hits){

        indexService.update3(id,hits);
        return Result.success();
    }
}
